package com.wcgksoft.controller.plant;


import com.wcgksoft.common.util.FileUtil;
import com.wcgksoft.common.util.Layui;
import com.wcgksoft.common.util.MultipartFileToFile;
import com.wcgksoft.controller.plant.qrcode.QRCodeUtil;
import com.wcgksoft.entity.Plant.Plant;
import com.wcgksoft.entity.Plant.PlantEncyclopedias;
import com.wcgksoft.entity.Plant.PlantType;
import com.wcgksoft.entity.system.EnterpriseProject;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.plant.PlantEncyclopediasService;
import com.wcgksoft.service.plant.PlantService;
import com.wcgksoft.service.plant.PlantTypeService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 植物百科
 *
 * @author Administrator
 */
@Controller
@RequestMapping("plantencyclopedias")
public class PlantEncyclopediasController {

    @Autowired
    private PlantEncyclopediasService plantEncyclopediasService;
    @Autowired
    private PlantTypeService plantTypeService;
    @Autowired
    private PlantService plantService;

    /**
     * 列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list")
    public Layui list(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "limit", required = false) Integer limit,
            @RequestParam(value = "keyWord", required = false) String keyWord) throws UnsupportedEncodingException {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        DetachedCriteria criteria = DetachedCriteria.forClass(PlantEncyclopedias.class);
        criteria.add(Restrictions.eq("enterpriseid", sysUser.getEnterprise().getId()));
       if(StringUtils.isNotEmpty(keyWord)) {
//            keyWord = new String(keyWord.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
            criteria.add(Restrictions.like("name", keyWord, MatchMode.ANYWHERE));
        }
        List<PlantEncyclopedias> list = plantEncyclopediasService.findByConditions(criteria, page, limit);
        return Layui.data(0, "查询成功", plantEncyclopediasService.getTotalCount(), list);
    }

    /*
     * 修改
     *
     * */
    @ResponseBody
    @RequestMapping(value = "update")
    public Layui update(HttpServletRequest request, @RequestParam(value = "data", required = false) String data,
                            @RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
        JSONObject param = JSONObject.fromObject(data);
        if (param == null) {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {
            PlantEncyclopedias plantEncyclopedias = (PlantEncyclopedias) JSONObject.toBean(JSONObject.fromObject(data), PlantEncyclopedias.class);
            if (null != file) {
                FileUtil fileUtil = new FileUtil();
                String uploadFile = fileUtil.uploadFile(MultipartFileToFile.multipartFileToFile(file), "plantEncyclopedias" + plantEncyclopedias.getId());
                plantEncyclopedias.setImage(uploadFile);
            }
            // 存放在二维码中的内容
            String text = plantEncyclopedias.getName();
            // 嵌入二维码的图片路径
            String imgPath = plantEncyclopedias.getImage();
            // 生成的二维码的路径及名称 修改到tomcat路径下
            String destPath = "C:/qrcode/"+plantEncyclopedias.getId()+".jpg";
            //生成二维码
            QRCodeUtil.encode(text, imgPath, destPath, true);
            plantEncyclopedias.setQrCode(destPath);
            // 解析二维码
            // String str = QRCodeUtil.decode(destPath);

            String type = String.valueOf(param.get("type_id"));
            Integer type_id=Integer.valueOf(type);
            PlantType byId = plantTypeService.findById(type_id);
            plantEncyclopedias.setPlantType(byId);
            SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
            plantEncyclopedias.setEnterpriseid(sysUser.getEnterprise().getId());
            plantEncyclopediasService.saveOrUpdate(plantEncyclopedias);
            return Layui.data(0, "修改成功", 1, Layui.addList(plantEncyclopedias));
        }
    }
    /*
     * 删除
     *
     * */
    @ResponseBody
    @RequestMapping(value = "delete")
    public Layui delete(@RequestParam(value = "id") Integer id) {
        PlantEncyclopedias byId = plantEncyclopediasService.findById(id);


        DetachedCriteria criteria = DetachedCriteria.forClass(Plant.class);
        criteria.createAlias("plantEncyclopedias", "plantEncyclopedias");
        criteria.add(Restrictions.eq("plantEncyclopedias.id",byId.getId()));
        List<Plant> list = plantService.findByConditions(criteria);
        if(list.size()>0){
            for(int i=0;i<list.size();i++){
                plantService.delete(list.get(i));
            }
        }
        plantEncyclopediasService.delete(byId);

        return Layui.data(1, "删除成功", 1, null);
    }

    /*
  查询植物分类
  */
    @ResponseBody
    @RequestMapping(value = "getType")
    public List<PlantType> getType() {
        DetachedCriteria criteria = DetachedCriteria.forClass(PlantType.class);

        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        criteria.add(Restrictions.eq("enterpriseid", sysUser.getEnterprise().getId()));
        criteria.add(Restrictions.eq("state", 0));
        List<PlantType> list = plantTypeService.findByConditions(criteria);
        return list;
    }


    /**
     * 植物查询列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "lists")
    public Layui lists(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "limit", required = false) Integer limit,
            @RequestParam(value = "greedlandid", required = false) Integer greedlandid,
            @RequestParam(value = "keyWord", required = false) String keyWord) {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        DetachedCriteria criteria = DetachedCriteria.forClass(PlantEncyclopedias.class);
        criteria.add(Restrictions.eq("enterpriseid", sysUser.getEnterprise().getId()));
        if(StringUtils.isNotEmpty(keyWord)) {
            criteria.add(Restrictions.like("name", keyWord, MatchMode.ANYWHERE));
        }
        List<PlantEncyclopedias> list = plantEncyclopediasService.findByConditions(criteria, page, limit);

        DetachedCriteria criteriap = DetachedCriteria.forClass(Plant.class);

        criteria.createAlias("greedLand", "greedLand");
        criteriap.add(Restrictions.eq("greedLand.id", greedlandid));
        List<Plant> listp = plantService.findByConditions(criteriap);

        if(list.size()>0){
            for(int i=0;i<list.size();i++){
                    if(listp.size()>0){
                        for(int j=0;j<listp.size();j++){
                            if(list.get(i).getId().equals(listp.get(j).getPlantEncyclopedias().getId())){
                                list.remove(i);
                            }
                        }
                    }
            }
        }



        return Layui.data(0, "查询成功", plantEncyclopediasService.getTotalCount(), list);
    }
}
